package com.sys.controller;

import com.sys.common.aop.Operation;
import com.sys.common.constant.Constant;
import com.sys.common.util.ResponseDateUtil;
import com.sys.common.util.ResultBuilder;
import com.sys.pojo.Dept;
import com.sys.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Api(tags = {"部门管理"})
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/listAllDept")
    @RequiresPermissions("dept:list")
    @ApiOperation("获取所有部门")
    @Operation("获取所有部门")
    public ResponseDateUtil getAllDept(@RequestBody Dept dept) {
        return ResultBuilder.success(deptService.findListByPage(dept));
    }

    @PostMapping("/listAllDeptName")
    @ApiOperation("获取所有部门名称")
    public ResponseDateUtil listAllDeptName() {
        return ResultBuilder.success(deptService.getAllDept());
    }

    @PostMapping("/addDept")
    @RequiresPermissions("dept:add")
    @ApiOperation("添加部门")
    @Operation("添加部门")
    public ResponseDateUtil addDept(@RequestBody Dept dept) {
        if (deptService.add(dept) > 0) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }

    @PutMapping("/updDept")
    @RequiresPermissions("dept:upd")
    @ApiOperation("更新部门信息")
    @Operation("更新部门信息")
    public ResponseDateUtil updDept(@RequestBody Dept dept) {
        if (deptService.updateData(dept) > 0) {
            return ResultBuilder.success(Constant.UPDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.UPDFAILED);
        }
    }

    @DeleteMapping("/delDept")
    @RequiresPermissions("dept:del")
    @ApiOperation("删除部门信息")
    @Operation("删除部门信息")
    public ResponseDateUtil delDept(@RequestParam("id") Integer id) {
        if (deptService.delete(id) > 0) {
            return ResultBuilder.success(Constant.DELSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.DELFAILED);
        }
    }

}
